

let glmaxtrix = require('gl-matrix');
let vec3 = glmaxtrix.vec3;
let mat4 = glmaxtrix.mat4;
let Vec2 = require('./vec2');

let DEG_TO_RAD = function () {
    let v = Math.PI / 180;
    return function (degress) {
        return v * degress;
    }
}();

class Matrix{

    constructor() {
        this.data = mat4.create();
    }

    identity(){
        mat4.identity(this.data);
    }

    copy(other){
        mat4.copy(this.data,other.data);
    }

    static ortho(left,right,bottom,top){
        let ret = new Matrix();
        mat4.ortho(ret.data,left,right,bottom,top,1,1000);
        return ret;
    }

    static defaultLookAt(){
        let ret = new Matrix();
        let eye = vec3.fromValues(0,0,2);
        mat4.lookAt(ret.data,eye,vec3.create(),vec3.fromValues(0,1,0));
        return ret;
    }

    static mul(out,m1,m2){
        mat4.multiply(out.data,m1.data,m2.data);
    }

    // buildFromTRS(pos,degress,scale){
    //     // TODO 可优化
    //     mat4.fromTranslation(Matrix._tmpT.data,vec3.fromValues(pos.x,pos.y,0));
    //     mat4.fromZRotation(Matrix._tmpR.data,DEG_TO_RAD(degress));
    //     mat4.fromScaling(Matrix._tmpS.data,vec3.fromValues(scale.x,scale.y,1));
    //     mat4.mul(this.data,Matrix._tmpT.data,Matrix._tmpR.data);
    //     mat4.mul(this.data,this.data,Matrix._tmpS.data);
    //     return this;
    // }
    // buildFromProperty(prop){
    //     mat4.fromTranslation(Matrix._tmpT.data,vec3.fromValues(prop._x,prop._y,0));
    //     mat4.fromZRotation(Matrix._tmpR.data,DEG_TO_RAD(prop._degress));
    //     mat4.fromScaling(Matrix._tmpS.data,vec3.fromValues(prop._sx,prop._sy,1));
    //     mat4.mul(this.data,Matrix._tmpT.data,Matrix._tmpR.data);
    //     mat4.mul(this.data,this.data,Matrix._tmpS.data);
    //     return this;
    // }

    // /**
    //  * @param node
    //  * @return {Matrix}
    //  */
    // buildFromNode(node){
    //     mat4.fromTranslation(Matrix._tmpT.data,vec3.fromValues(node.x,node.y,0));
    //     mat4.fromZRotation(Matrix._tmpR.data,DEG_TO_RAD(node.degress));
    //     mat4.fromScaling(Matrix._tmpS.data,vec3.fromValues(node.sx,node.sy,1));
    //     mat4.mul(this.data,Matrix._tmpT.data,Matrix._tmpR.data);
    //     mat4.mul(this.data,this.data,Matrix._tmpS.data);
    //     return this;
    // }

    buildFromNode2(node){
        let rad = DEG_TO_RAD(node.degress);
        let cos = Math.cos(rad);
        let sin = Math.sin(rad);
        let sx = node.sx;
        let sy = node.sy;
        mat4.set(this.data,cos * sx,sin * sx,0,0,
            -sin * sy,cos * sy,0,0,
            0,0,1,1,
            node.x,node.y,0,1);
    }

    translatePoint2(x,y){
        let newX = this.data[0] * x + this.data[4] * y + this.data[12];
        let newY = this.data[1] * x + this.data[5] * y + this.data[13];
        return new Vec2(newX,newY);
    }

    getInverse(){
        let invMat = new Matrix();
        mat4.invert(invMat.data,this.data);
        return invMat;
    }
}

Matrix._tmpT = new Matrix();
Matrix._tmpR = new Matrix();
Matrix._tmpS = new Matrix();

module.exports = Matrix;



//
//
// let glmaxtrix = require('gl-matrix');
// let vec3 = glmaxtrix.vec3;
// let mat4 = glmaxtrix.mat4;
//
//
// let DEG_TO_RAD = function () {
//     let v = Math.PI / 180;
//     return function (degress) {
//         return v * degress;
//     }
// }();
//
// class Matrix{
//
//     constructor() {
//         this.data = mat4.create();
//     }
//
//     identity(){
//         mat4.identity(this.data);
//     }
//
//     copy(other){
//         mat4.copy(this.data,other.data);
//     }
//
//     static ortho(left,right,bottom,top){
//         let ret = new Matrix();
//         mat4.ortho(ret.data,left,right,bottom,top,1,1000);
//         return ret;
//     }
//
//     static defaultLookAt(){
//         let ret = new Matrix();
//         let eye = vec3.fromValues(0,0,2);
//         mat4.lookAt(ret.data,eye,vec3.create(),vec3.fromValues(0,1,0));
//         return ret;
//     }
//
//     static mul(out,m1,m2){
//         mat4.multiply(out.data,m1.data,m2.data);
//     }
//
//     buildFromTRS(pos,degress,scale){
//         // TODO 可优化
//         mat4.fromTranslation(Matrix._tmpT.data,vec3.fromValues(pos.x,pos.y,0));
//         mat4.fromZRotation(Matrix._tmpR.data,DEG_TO_RAD(degress));
//         mat4.fromScaling(Matrix._tmpS.data,vec3.fromValues(scale.x,scale.y,1));
//         mat4.mul(this.data,Matrix._tmpT.data,Matrix._tmpR.data);
//         mat4.mul(this.data,this.data,Matrix._tmpS.data);
//         return this;
//     }
//
//     buildFromProperty(prop){
//         mat4.fromTranslation(Matrix._tmpT.data,vec3.fromValues(prop._x,prop._y,0));
//         mat4.fromZRotation(Matrix._tmpR.data,DEG_TO_RAD(prop._degress));
//         mat4.fromScaling(Matrix._tmpS.data,vec3.fromValues(prop._sx,prop._sy,1));
//         mat4.mul(this.data,Matrix._tmpT.data,Matrix._tmpR.data);
//         mat4.mul(this.data,this.data,Matrix._tmpS.data);
//         return this;
//     }
// }
//
// Matrix._tmpT = new Matrix();
// Matrix._tmpR = new Matrix();
// Matrix._tmpS = new Matrix();
//
// module.exports = Matrix;
